import java.util.Stack;

class Demo {
    public boolean isValid(String s){
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++){
            char ch = s.charAt(i);
            if(ch == '{' || ch == '(' || ch == '['){
                stack.push(ch);
            }else{
                //说明ch里面是右括号
                if(stack.empty()){
                    //右括号多！
                    return false;
                }
                char top = stack.peek();
                if(ch == ')' && top == '(' || ch == ']' && top == '[' || ch == '}' && top == '{'){
                    //只能说明 当前字符是匹配的
                    stack.pop();
                }else{
                    //左右括号不匹配
                    return false;
                }
            }
        }
        if(stack.empty()){
            return true;
        }else {
            //左括号多
            return false;
        }
    }
}
